﻿Imports FinanceiroBLL
Imports FinanceiroDAL
Public Class PrestadoresFRM
    Dim objBLL As New PrestadoresBLL
    Public Op As String
    Public codigo As Integer
    ' Public CodCargo As Integer
#Region "Carrega Campos"
    Sub carregacampos(ByVal codigo As Integer)
        objBLL = New PrestadoresBLL(codigo)
        LBCodigo.Text = "Código: " & objBLL.pre_codigo
        LBPrestadores.Text = "Prestadores" 'objBLL.pre_descricao
        TNome.Text = objBLL.pre_descricao
        CBCentroCusto.SelectedValue = objBLL.ccc_codigo
        '  mtb = predtcadastro
        TObs.Text = objBLL.pre_obs
        CBTpPessoa.SelectedValue = objBLL.tpe_codigo
        MTBCPF.Text = objBLL.pre_cpf
        TRg.Text = objBLL.pre_rg
        MTBCNPJ.Text = objBLL.pre_cnpj
        TIe.Text = objBLL.pre_inscricao
        MTBResidencial.Text = objBLL.pre_telres
        MTBComercial.Text = objBLL.pre_telcom
        MTBCelular.Text = objBLL.pre_telcel
        MTBFax.Text = objBLL.pre_telfax
        TEndereco.Text = objBLL.pre_end
        TNum.Text = objBLL.pre_numero
        TBairro.Text = objBLL.pre_bairro
        MTBCep.Text = objBLL.pre_cep
        TCidade.Text = objBLL.pre_cidade
        CBUf.SelectedValue = objBLL.est_uf
        TEmail.Text = objBLL.pre_email
        CBStatus.SelectedValue = objBLL.sta_codigo
    End Sub
#End Region
#Region "Limpa Campos"
    Sub limpacampos()
        TNome.Text = ""
        CBCentroCusto.SelectedValue = 1
        '  mtb = predtcadastro
        TObs.Text = ""
        CBTpPessoa.SelectedValue = 1
        MTBCPF.Text = ""
        TRg.Text = ""
        MTBCNPJ.Text = ""
        TIe.Text = ""
        MTBResidencial.Text = ""
        MTBComercial.Text = ""
        MTBCelular.Text = ""
        MTBFax.Text = ""
        TEndereco.Text = ""
        TNum.Text = ""
        TBairro.Text = ""
        MTBCep.Text = ""
        TCidade.Text = ""
        CBUf.SelectedValue = ""
        TEmail.Text = ""
        CBStatus.SelectedValue = 1
        Me.objBLL = Nothing
    End Sub
#End Region
#Region "Load" '"ccc_codigo", "ccc_descricao", "ccc_tipo <> 'Entrada'", "sta_codigo < 3"
    Private Sub PrestadoresFRM_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        objconexao.carregaCombo(CBCentroCusto, "Financeiro.Ofi_ClassCentroCusto", "ccc_codigo", "ccc_descricao", "ccc_tipo <> 'Entrada' and sta_codigo < 3 and ccc_codigo <> 1", "")
        objconexao.carregaCombo(CBStatus, "Generico.Gen_Status", "sta_codigo", "sta_descricao", "sta_codigo < 3", "sta_codigo")
        objconexao.carregaCombo(CBTpPessoa, "Generico.Gen_TipoPessoa", "tpe_codigo", "tpe_descricao", "tpe_codigo < 3", "tpe_codigo")
        objconexao.carregaCombo(CBUf, "Generico.Gen_Estado", "est_uf", "est_uf", "", "")
        CBCentroCusto.SelectedValue = 1
        CBStatus.SelectedValue = 1
        CBTpPessoa.SelectedValue = 1
        CBUf.SelectedValue = 1
        limpacampos()
        If (Op = "I") Then
            LBCodigo.Visible = False
            LBPrestadores.Text = "Prestadores"
            LBCodigo.Text = 0
            CBStatus.Enabled = False
        Else
            LBCodigo.Visible = True
            carregacampos(codigo)
        End If
    End Sub
#End Region
#Region "Salvar"
    Private Sub TSBSalvar_Click(sender As System.Object, e As System.EventArgs) Handles TSBSalvar.Click
        Try

            objBLL = New PrestadoresBLL(codigo, StrConv(Trim(TNome.Text), VbStrConv.ProperCase), CBCentroCusto.SelectedValue, Trim(TObs.Text), CBTpPessoa.SelectedValue,
                                        Trim(MTBCPF.Text).Replace("-", "").Replace(" ", "").Replace(",", ""), Trim(TRg.Text), Trim(MTBCNPJ.Text).Replace("/", "").Replace("-", "").Replace(" ", "").Replace(",", ""),
                                        Trim(TIe.Text), Trim(MTBResidencial.Text).Replace("(", "").Replace(")", "").Replace("-", "").Replace(" ", ""), Trim(MTBComercial.Text).Replace("(", "").Replace(")", "").Replace("-", "").Replace(" ", ""),
                                        Trim(MTBCelular.Text).Replace("(", "").Replace(")", "").Replace("-", "").Replace(" ", ""), Trim(MTBFax.Text).Replace("(", "").Replace(")", "").Replace("-", "").Replace(" ", ""),
                                        StrConv(Trim(TEndereco.Text), VbStrConv.ProperCase), TNum.Text, StrConv(Trim(TBairro.Text), VbStrConv.ProperCase), MTBCep.Text.Replace(" ", "").Replace("-", ""), StrConv(Trim(TCidade.Text), VbStrConv.ProperCase),
                                        CBUf.SelectedValue, StrConv(Trim(TComplemento.Text), VbStrConv.ProperCase), Trim(TEmail.Text), CBStatus.SelectedValue)


            If Op = "I" Then
                objBLL.InserirAlterar()
                MessageBox.Show("Prestador inserido com sucesso", "Sucesso", MessageBoxButtons.OK, MessageBoxIcon.Information)
                limpacampos()
                Me.Close()
            Else
                objBLL.InserirAlterar()
                MessageBox.Show("Prestador alterado com sucesso", "Sucesso", MessageBoxButtons.OK, MessageBoxIcon.Information)
                limpacampos()
                Me.Close()
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Atenção", MessageBoxButtons.OK, MessageBoxIcon.Information)
        End Try
    End Sub
#End Region

    Private Sub TSBVoltar_Click(sender As System.Object, e As System.EventArgs) Handles TSBVoltar.Click
        Me.Close()
    End Sub

    Private Sub CBTpPessoa_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles CBTpPessoa.SelectedIndexChanged
        If (CBTpPessoa.SelectedIndex = 0) Then 'pF
            LBCnpj.Visible = False
            MTBCNPJ.Visible = False
            LBIe.Visible = False
            TIe.Visible = False
            LBRg.Visible = True
            LBCpf.Visible = True
            MTBCPF.Visible = True
            LBCpf.Visible = True
            TRg.Visible = True
        ElseIf (CBTpPessoa.SelectedIndex = 1) Then
            LBCpf.Visible = False
            MTBCPF.Visible = False
            LBRg.Visible = False
            TRg.Visible = False
            LBCnpj.Visible = True
            MTBCNPJ.Visible = True
            LBIe.Visible = True
            TIe.Visible = True
        End If
        MTBCNPJ.Text = ""
        TIe.Text = ""
        TRg.Text = ""
        MTBCPF.Text = ""

    End Sub
#Region "Validação dos campos"

    Private Sub TCidade_KeyPress(sender As System.Object, e As System.Windows.Forms.KeyPressEventArgs) Handles TCidade.KeyPress
        If Not Char.IsLetter(e.KeyChar) And Not e.KeyChar = vbBack And Not e.KeyChar = "." And Not e.KeyChar = "," And e.KeyChar = "" Then
            e.Handled = True
        End If
    End Sub


    Private Sub TRg_KeyPress_1(sender As System.Object, e As System.Windows.Forms.KeyPressEventArgs) Handles TRg.KeyPress
        If Not Char.IsNumber(e.KeyChar) And Not e.KeyChar = vbBack Then
            e.Handled = True
        End If
    End Sub

    Private Sub TBCnpj_KeyPress(sender As System.Object, e As System.Windows.Forms.KeyPressEventArgs) Handles TBCnpj.KeyPress
        If Not Char.IsNumber(e.KeyChar) And Not e.KeyChar = vbBack Then
            e.Handled = True
        End If
    End Sub

    Private Sub MTBCPF_KeyPress(sender As System.Object, e As System.Windows.Forms.KeyPressEventArgs) Handles MTBCPF.KeyPress
        If Not Char.IsNumber(e.KeyChar) And Not e.KeyChar = vbBack Then
            e.Handled = True
        End If
    End Sub

    Private Sub MTBCNPJ_KeyPress(sender As System.Object, e As System.Windows.Forms.KeyPressEventArgs) Handles MTBCNPJ.KeyPress
        If Not Char.IsNumber(e.KeyChar) And Not e.KeyChar = vbBack Then
            e.Handled = True
        End If
    End Sub
#End Region

End Class